package com.haoxin.ai_course.modules.course.controller.admin;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haoxin.ai_course.common.annotation.SysLog;
import com.haoxin.ai_course.common.base.AbstractController;
import com.haoxin.ai_course.common.utils.DateUtil;
import com.haoxin.ai_course.common.utils.EasyExcelUtil;
import com.haoxin.ai_course.common.utils.R;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsCourse;
import com.haoxin.ai_course.modules.course.entity.VO.AcStatisticsCourseExcelVO;
import com.haoxin.ai_course.modules.course.service.AcStatisticsCourseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;



/**
 * 课程统计表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/course/acstatisticscourse")
@Api(value="api",tags="后台-课程统计管理")
public class AcStatisticsCourseController  extends AbstractController {
    private final  AcStatisticsCourseService acStatisticsCourseService;
    private final  MapperFacade mapperFacade;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation("课程统计列表")
    @RequiresPermissions("course:acstatisticscourse:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        QueryWrapper<AcStatisticsCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(AcStatisticsCourse::getCourseId);
        IPage<AcStatisticsCourse> sysConfigList = acStatisticsCourseService.page(mpPageConvert.<AcStatisticsCourse>pageParamConvert(params),queryWrapper);
        return R.ok().put("page", mpPageConvert.pageValueConvert(sysConfigList));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("course:acstatisticscourse:info")
    public R info(@PathVariable("id") Integer id){
        return R.ok().put("acStatisticsCourse", acStatisticsCourseService.getById(id));
    }


    /**
     * 新增课程统计表
     */
    @SysLog("新增课程统计表数据")
    @RequestMapping("/save")
    @RequiresPermissions("course:acstatisticscourse:save")
    public R save(@RequestBody AcStatisticsCourse acStatisticsCourse){
        acStatisticsCourseService.save(acStatisticsCourse);
        return R.ok();
    }


    /**
     * 修改
     */
    @SysLog("修改课程统计表数据")
    @RequestMapping("/update")
    @RequiresPermissions("course:acstatisticscourse:update")
    public R update(@RequestBody AcStatisticsCourse acStatisticsCourse){
		acStatisticsCourseService.updateById(acStatisticsCourse);
        return R.ok();
    }


    /**
     * 删除
     */
    @SysLog("删除课程统计表数据")
    @RequestMapping("/delete")
    @RequiresPermissions("course:acstatisticscourse:delete")
    public R delete(@RequestBody Integer[] ids){
		acStatisticsCourseService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    @GetMapping("exportExcel")
    @ApiOperation("导出excel")
	public void exportExcel(HttpServletResponse response) {
		List<AcStatisticsCourse> acStatisticsCourseList = acStatisticsCourseService.list();
		List<AcStatisticsCourseExcelVO> acStatisticsCourseExcelVOList=mapperFacade.mapAsList(acStatisticsCourseList, AcStatisticsCourseExcelVO.class);
		try {
			EasyExcelUtil.exprotExcel(response, acStatisticsCourseExcelVOList, "课程统计表-" +DateUtil.localDateTimeFormat(LocalDateTime.now(), "yyyyMMdd"),
			"课程统计" ,
			AcStatisticsCourseExcelVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
